package com.tianluo.tianluobookstore.service.impl;

import com.tianluo.tianluobookstore.service.RedisService;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {

    //系统标记前缀
    private static final String PREFIX_KEY = "tianluo";

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void setNx(String key, String value, long expireTime) {
        key = PREFIX_KEY + ":" + key;
        RMapCache<String, String> cache = redissonClient.getMapCache("tianLuoCache");
        // 设置缓存值，TTL 为 10 秒，最大闲置时间为 5 秒
        cache.put(key, value, expireTime, TimeUnit.SECONDS);
    }

    @Override
    public String get(String key) {
        key = PREFIX_KEY + ":" + key;
        RMapCache<String, String> cache = redissonClient.getMapCache("tianLuoCache");
        return cache.get(key);
    }

}
